Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instrument adapter queries for external programs. #105

Merged
merged 1 commit into from
Jul 9, 2024

Conversation

benk-gc
Copy link
Contributor

@benk-gc benk-gc commented Jul 5, 2024

When executing queries via the adapter, we may not recieve the instrumentation we expect. The ActiveRecord adapter uses a raw connection which bypasses the ActiveRecord instrumentation layer and instead talks directly to PG, making it impossible to know what queries are being executed.

This change cribs heavily from the implementation of the log method on the AbstractAdapter class in ActiveRecord to make it easy to add instrumentation to an adapter. By default the instrumenter is set to nil, and instrumentation will be a no-op; otherwise the query will be instrumented via the instrument method and passed the relevant payload to handle. The structure of this is deliberately similar to that of the event emitted by ActiveRecord's sql.active_record notification.

@benk-gc benk-gc force-pushed the benk--instrument-que-adapter-queries branch from feed35c to acb446f Compare July 5, 2024 12:15
@benk-gc benk-gc marked this pull request as ready for review July 5, 2024 12:16
@benk-gc benk-gc force-pushed the benk--instrument-que-adapter-queries branch 2 times, most recently from 491f54a to 976ee88 Compare July 5, 2024 16:23
Copy link

@Tabby Tabby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine AFAICT :)

When executing queries via the adapter, we may not recieve the
instrumentation we expect. The ActiveRecord adapter uses a raw
connection which bypasses the ActiveRecord instrumentation layer and
instead talks directly to PG, making it impossible to know what queries
are being executed.

This change cribs heavily from the implementation of the `log` method on
the `AbstractAdapter` class in ActiveRecord to make it easy to add
instrumentation to an adapter. By default the instrumenter is set to
`nil`, and instrumentation will be a no-op; otherwise the query will be
instrumented via the `instrument` method and passed the relevant payload
to handle. The structure of this is deliberately similar to that of the
event emitted by ActiveRecord's `sql.active_record` notification.
@benk-gc benk-gc force-pushed the benk--instrument-que-adapter-queries branch from 976ee88 to af8d14c Compare July 9, 2024 10:37
@benk-gc benk-gc merged commit e9db809 into master Jul 9, 2024
6 checks passed
@benk-gc benk-gc deleted the benk--instrument-que-adapter-queries branch July 9, 2024 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants